home *** CD-ROM | disk | FTP | other *** search
/ Future Workshop / Future Workshop.iso / multimed / qtw111 / pviewer / viewmain.c < prev    next >
C/C++ Source or Header  |  1994-01-11  |  17KB  |  522 lines

  1.  
  2. // ---------------------------------------------------------------------
  3. //
  4. // ViewMain.c - Picture Viewer - QuickTime for Windows
  5. //
  6. //              Version 1.0
  7. //
  8. //              (c) 1988-1992 Apple Computer, Inc. All Rights Reserved.
  9. //
  10. // ---------------------------------------------------------------------
  11.  
  12.  
  13. // Includes
  14. // --------
  15. #include <Windows.h> // Required by Windows
  16.  
  17. #include <qtw.h>   // Interface to QuickTime
  18. #include <qtole.h> // Interface to qtole dll
  19.  
  20. #include "common.h" // Interface to common.c routines
  21.  
  22. #include "viewer.h"  // Interface to other *.c files
  23. #include "viewer.hr" // Defines used in *.rc files
  24. #include "picture.h" // Needed for registering
  25.                      // child window controls
  26.  
  27.  
  28. // Message-Persistent Data
  29. // -----------------------
  30. static struct // Hungarian notation: g
  31.   {HINSTANCE     hInstance;          // Instance handle
  32.    HINSTANCE     hResources;         // Resource-only DLL handle
  33.    HWND          hwndFrame;          // Frame window handle
  34.    HWND          hwndClient;         // MDI client window
  35.    HMENU         hMenu;              // Frame window menu
  36.    HACCEL        hAccel;             // Frame window accelerators
  37.    HWND          hActiveModelessDlg; // Handle of active modeless dlg if any
  38.    BOOL          fPalettized;        // Palettized device
  39.    QTOLE_OLEDATA qtoleOleData;       // OLE data struct
  40.   } g;
  41.  
  42.  
  43. // Internal Function Declarations
  44. // ------------------------------
  45. static LPSTR NEAR ViewerParseCmdLine    (LPSTR);
  46. static  BOOL NEAR ViewerInitAppl        (VOID);
  47. static  HWND NEAR ViewerInitInst        (HINSTANCE, LPSTR, int);
  48. static  LONG NEAR ViewerTerminateInst   (VOID);
  49. static  BOOL NEAR DoQuickTimeInit       (HINSTANCE, LPSTR, LPINT);
  50. static  VOID NEAR KillQuickTime         (VOID);
  51.  
  52. // Function: WinMain - Required Windows "Main" Routine
  53. // --------------------------------------------------------------------
  54. // Parameters: As required by Microsoft Windows
  55. //
  56. // Returns:    As required by Microsoft Windows
  57. // --------------------------------------------------------------------
  58. int PASCAL WinMain( HINSTANCE hInstance,
  59.                  HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  60.  
  61. // Define function data
  62.  
  63. {
  64.     MSG  msg; // Message
  65.  
  66.     g.hInstance = hInstance; // Initialize global data
  67.  
  68.     // Load resource-only DLL
  69.     if( !(g.hResources = CommonGetLocalizedResources
  70.         ( VIEWER_ROOT_NAME, hInstance, VIEWER_STRING_NOMEMORY )))
  71.         return 0;
  72.  
  73.     // Perform one-time initialization
  74.  
  75.     if( !hPrevInstance && !ViewerInitAppl() )
  76.         return 0;
  77.  
  78.     // Perform initializations that apply to a specific instance and
  79.     // create window
  80.     if( !ViewerInitInst( hPrevInstance, lpCmdLine, nCmdShow ))
  81.         return 0;
  82.  
  83.  
  84.     // main message loop
  85.     while( GetMessage( &msg, NULL, NULL, NULL )) {
  86.         if( !g.hActiveModelessDlg ||
  87.             !IsDialogMessage( g.hActiveModelessDlg, &msg )) {
  88.             if( !g.hwndClient ||
  89.                 !TranslateMDISysAccel( g.hwndClient, &msg )) {
  90.                 if( !g.hwndFrame ||
  91.                     !TranslateAccelerator( g.hwndFrame, g.hAccel, &msg )) {
  92.                     TranslateMessage(&msg);
  93.                     DispatchMessage (&msg);
  94.                 }
  95.             }
  96.         }
  97.     }
  98.  
  99.     // Cleanup Picture Viewer
  100.  
  101.     ViewerTerminateInst();
  102.  
  103.     return msg.wParam;
  104. }
  105.  
  106.  
  107. // Function: ViewerInitAppl - Perform One-time Initialization
  108. // --------------------------------------------------------------------
  109. // Parameters: None
  110. //
  111. // Returns:    TRUE if OK, else FALSE
  112. // --------------------------------------------------------------------
  113. static BOOL NEAR ViewerInitAppl( VOID )
  114.  
  115. {
  116.     WNDCLASS wc; // Window class information
  117.  
  118.     // Register the frame (main) window class
  119.  
  120.     wc.style         = 0;
  121.     wc.lpfnWndProc   = ViewerFrameWndProc;
  122.     wc.cbClsExtra    = 0;
  123.     wc.cbWndExtra    = 0;
  124.     wc.hInstance     = g.hInstance;
  125.     wc.hIcon         = LoadIcon( g.hResources,
  126.         MAKEINTRESOURCE( VIEWER_VIEWER_ICON ));
  127.     wc.hCursor       = LoadCursor( NULL, IDC_ARROW );
  128.     wc.hbrBackground = (HBRUSH) ( COLOR_APPWORKSPACE + 1 );
  129.     wc.lpszMenuName  = NULL;
  130.     wc.lpszClassName = VIEWER_FRAME_CLASS;
  131.  
  132.     if( !RegisterClass( &wc ))
  133.         return FALSE;
  134.  
  135.     // Register the picture window class
  136.  
  137.     wc.style         = 0;
  138.     wc.lpfnWndProc   = ViewerPictureWndProc;
  139.     wc.cbClsExtra    = 0;
  140.     wc.cbWndExtra    = sizeof( VOID NEAR * );
  141.     wc.hInstance     = g.hInstance;
  142.     wc.hIcon         = NULL;
  143.     wc.hCursor       = LoadCursor( NULL, IDC_ARROW );
  144.     wc.hbrBackground = PICBACKGRNDBRUSH;
  145.     wc.lpszMenuName  = NULL;
  146.     wc.lpszClassName = VIEWER_PICTURE_CLASS;
  147.  
  148.     if( !RegisterClass( &wc ))
  149.         return FALSE;
  150.  
  151.     // Register the child classes used by the picture windows
  152.     // This function is in PictKids.c
  153.  
  154.     if( !RegisterChildControls( g.hInstance ))
  155.         return FALSE;
  156.  
  157.     return TRUE;
  158. }
  159.  
  160.  
  161. // Function: ViewerInitInst - Perform Instance Initialization
  162. // --------------------------------------------------------------------
  163. // Parameters: HINSTANCE hPrevInstance;  Previous instance
  164. //             LPSTR     lpCmdLine;      -->Command line arguments
  165. //             int       nCmdShow;       Parameter for first ShowWindow()
  166. //
  167. // Returns:    HWND      hwndFrame;      Frame window handle
  168. //                                       or NULL if initialization failed
  169. // --------------------------------------------------------------------
  170. static HWND NEAR ViewerInitInst
  171.               ( HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  172.  
  173. {
  174.     HDC hdcIC;
  175.  
  176.     // Do QuickTime Initializations.
  177.     if( !DoQuickTimeInit( hPrevInstance, lpCmdLine, &nCmdShow ))
  178.         return NULL;
  179.  
  180.     // Is this a palettized device?
  181.     if( hdcIC = CreateIC( "DISPLAY", NULL, NULL, NULL )) {
  182.         g.fPalettized = 
  183.             ( GetDeviceCaps( hdcIC, RASTERCAPS ) & RC_PALETTE );
  184.         DeleteDC( hdcIC );
  185.     }
  186.  
  187.     // get menu and accelerators from localized resource
  188.     g.hAccel = LoadAccelerators( g.hResources,
  189.         MAKEINTRESOURCE( VIEWER_ACCELERATORS ));
  190.     g.hMenu  = LoadMenu( g.hResources,
  191.         MAKEINTRESOURCE( VIEWER_FRAME_MENU ));
  192.     if( !g.hAccel || !g.hMenu ) {
  193.         CommonTellUser( g.hResources, VIEWER_STRING_NOACCELORMENU,
  194.             VIEWER_STRING_CAPTION, MB_OK );
  195.         return NULL;
  196.     }
  197.  
  198.     // Create a main window for this application instance.
  199.     if( !(g.hwndFrame = CreateWindow( VIEWER_FRAME_CLASS, "",
  200.         WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  201.         CW_USEDEFAULT, CW_USEDEFAULT,
  202.         CW_USEDEFAULT, CW_USEDEFAULT,
  203.         NULL, g.hMenu, g.hInstance, NULL ))) {
  204.         CommonTellUser( g.hResources, VIEWER_STRING_NOWINDOW,
  205.             VIEWER_STRING_CAPTION, MB_OK );
  206.         return NULL;
  207.     }
  208.  
  209.     // Tell qtole.dll the server hwnd
  210.     QTOLE_SetApplicationHwnd( &g.qtoleOleData, g.hwndFrame ); 
  211.  
  212.     // save MDI client window created during WM_CREATE message processing
  213.     g.hwndClient = ViewerQueryClientWindow(); // this is in FrameWnd.c
  214.  
  215.     // Display the frame window
  216.     // If ole started the app, the window will be initially hidden
  217.     ShowWindow( g.hwndFrame, nCmdShow );
  218.     if( nCmdShow != SW_HIDE )
  219.         UpdateWindow( g.hwndFrame );
  220.  
  221.     // Check command line for picture file.
  222.     // Note: this must come after ShowWindow, UpdateWindow
  223.     if( lpCmdLine = ViewerParseCmdLine( lpCmdLine ))
  224.         SendMessage( g.hwndFrame, 
  225.         WM_VIEWER_CMDLINE, 0, (LPARAM) lpCmdLine );
  226.  
  227.     return g.hwndFrame;
  228. }
  229.  
  230. // Function: ViewerParseCmdLine - Parse the command line. Return -> to
  231. //                                first argument and ignore any extras.
  232. //                                The default extension is added if there
  233. //                                is no extension
  234. // --------------------------------------------------------------------
  235. // Parameters: LPSTR      lpCmdLine     command line pointer
  236. //
  237. // Returns:    LPSTR      lpCmdLine     command line pointer to first
  238. //                                      argument, else NULL
  239. // --------------------------------------------------------------------
  240. static LPSTR NEAR ViewerParseCmdLine( LPSTR lpCmdLine )
  241.  
  242. {
  243.     LPSTR  lpTemp;                        //  Temp pointer
  244.     char   szExtension[FILE_EXT_LEN + 1]; // Default file extension
  245.     BOOL   bExtension;                    // Extension flag
  246.  
  247.     // Command line is already in Ansi char set even if entered from
  248.     // DOS command line
  249.  
  250.     // remove any leading blanks
  251.     while( *lpCmdLine == ' ' )
  252.         lpCmdLine = AnsiNext( lpCmdLine );
  253.  
  254.     if( *lpCmdLine == '\0' )
  255.         return NULL;
  256.  
  257.     // look for blank or end of string
  258.     bExtension = FALSE;
  259.     lpTemp = lpCmdLine;
  260.     while( *lpTemp && (*lpTemp != ' ' )) {
  261.         if( *lpTemp == '.' )
  262.             bExtension = TRUE;
  263.         lpTemp = AnsiNext( lpTemp );
  264.     }
  265.  
  266.     *lpTemp = '\0';
  267.  
  268.     if( !bExtension ) {
  269.         LoadString( g.hResources,
  270.             VIEWER_STRING_FILEEXT, szExtension, sizeof( szExtension ));
  271.         lstrcat( lpCmdLine, szExtension );
  272.     }
  273.  
  274.     return lpCmdLine;
  275. }
  276.  
  277.  
  278. // Function: ViewerTerminateInst - Terminate Instance
  279. // --------------------------------------------------------------------
  280. // Parameters: VOID
  281. //
  282. // Returns:    Always 0L
  283. // --------------------------------------------------------------------
  284. static LONG NEAR ViewerTerminateInst( VOID )
  285.  
  286. {
  287.     // Clean up OLE
  288.     if( g.qtoleOleData.lpqtoleServer )
  289.         QTOLE_OLECleanUp( &g.qtoleOleData );
  290.  
  291.     // Cut the connections to QuickTime.
  292.     KillQuickTime();
  293.  
  294.     // Free the resource-only DLL
  295.     if( g.hResources && ( g.hInstance != g.hResources ))
  296.         FreeLibrary( g.hResources );
  297.  
  298.     return 0L;
  299. }
  300.  
  301.  
  302. // The next two functions are used to initialize and kill QuickTime
  303.  
  304. // Function: DoQuickTimeInit - Establishes connections to QuickTime
  305. // --------------------------------------------------------------------
  306. // Parameters: HINSTANCE  hPrevInstance  Previous instance
  307. //             LPSTR      lpCmdLine      -> command line
  308. //             LPINT      lpnCmdShow     Parameter for first ShowWindow()
  309. //
  310. // Returns:    BOOL       TRUE if OK, else FALSE
  311. // --------------------------------------------------------------------
  312. static BOOL NEAR DoQuickTimeInit
  313.             ( HINSTANCE hPrevInstance, LPSTR lpCmdLine, LPINT lpnCmdShow )
  314.  
  315. {
  316.     OSErr       oserr;     // Temp return value
  317.     WORD        wIDString; // ID of error message string
  318.     QTOLE_INIT  qtoleInit;
  319.     QTOLE_ERR   qtole_err;
  320.     char        szCaption[30];
  321.  
  322.     LoadString( g.hResources, VIEWER_STRING_CAPTION,
  323.         szCaption, sizeof( szCaption ));
  324.  
  325.     // Do OLE initialization before QTInitialize so we
  326.     // don't have to kill qt for install only
  327.     if( !( qtoleInit.fpServerCallBack = 
  328.         (QTOLEPROC) MakeProcInstance
  329.         ( (FARPROC) QTOLEServerCallBack, g.hInstance ))) {
  330.         CommonTellUser( g.hResources, VIEWER_STRING_NOMEMORY,
  331.             VIEWER_STRING_CAPTION, MB_OK );
  332.         return FALSE;
  333.     }
  334.     else {
  335.         qtoleInit.lStructSize           = sizeof( qtoleInit );
  336.         qtoleInit.lVersion              = VERSION_1;
  337.         qtoleInit.hInstance             = g.hInstance;
  338.         qtoleInit.hResources            = g.hResources;
  339.         qtoleInit.lpCmdLine             = lpCmdLine;
  340.         qtoleInit.lpClassName           = VIEWER_FRAME_CLASS;
  341.         qtoleInit.lpServerCaption       = szCaption;
  342.         qtoleInit.lpnCmdShow            = lpnCmdShow;
  343.         qtoleInit.wIDFirstString        = OLE_STRING_FIRST;
  344.         qtoleInit.wIDFirstDlg           = OLE_DLG_FIRST;
  345.         qtoleInit.bMultipleObjectServer = TRUE;
  346.  
  347.         g.qtoleOleData.lStructSize      = sizeof( g.qtoleOleData );
  348.         g.qtoleOleData.lVersion         = VERSION_1;
  349.         g.qtoleOleData.wObjectType      = PICTURE_OBJECT;
  350.  
  351.         if( QTOLE_OK != 
  352.             ( qtole_err = QTOLE_Initialize( &g.qtoleOleData, &qtoleInit ))) {
  353.             if( QTOLE_INSTALL_ONLY != qtole_err )
  354.                 CommonTellUser( g.hResources, 
  355.                 VIEWER_STRING_OLEINITFAILED,
  356.                 VIEWER_STRING_CAPTION, MB_OK );
  357.             return FALSE;
  358.         }
  359.     }
  360.  
  361.     if( ( oserr = QTInitialize( NULL )) != QTI_OK ) {
  362.         switch( oserr ) {
  363.             case QTI_FAIL_NOEXIST:
  364.                 wIDString = VIEWER_STRING_QTWNOEXIST;
  365.                 break;
  366.  
  367.             case QTI_FAIL_CORRUPTDLL:
  368.                 wIDString = VIEWER_STRING_QTWBADDLL;
  369.                 break;
  370.  
  371.             case QTI_FAIL_286:
  372.                 wIDString = VIEWER_STRING_QTW286;
  373.                 break;
  374.  
  375.             case QTI_FAIL_WIN30:
  376.                 wIDString = VIEWER_STRING_QTWWIN30;
  377.                 break;
  378.  
  379.             default:
  380.                 wIDString = VIEWER_STRING_QTWFAILED;
  381.                 break;
  382.         }
  383.  
  384.         CommonTellUser( g.hResources, wIDString,
  385.             VIEWER_STRING_CAPTION, MB_OK );
  386.         return FALSE;
  387.     }
  388.  
  389.     // This function call is necessary to make possible the use of
  390.     // the function GetMovieStickyError() that returns the error code
  391.     // for certain QTW functions.
  392.     if( EnterMovies() ) {
  393.         CommonTellUser( g.hResources, VIEWER_STRING_ENTMOVFAILED,
  394.             VIEWER_STRING_CAPTION, MB_OK );
  395.         return FALSE;
  396.     }
  397.  
  398.     return TRUE;
  399. }
  400.  
  401.  
  402. // Function: KillQuickTime - Cuts the connections to QuickTime
  403. // --------------------------------------------------------------------
  404. // Parameters: VOID
  405. //
  406. // Returns:    VOID
  407. // --------------------------------------------------------------------
  408. static VOID NEAR KillQuickTime( VOID )
  409.  
  410. {
  411.     ExitMovies();
  412.     QTTerminate();
  413.  
  414.     return;
  415. }
  416.  
  417.  
  418. //  This function is called by other modules whenever a modeless dialog
  419. //  is activated or deactivated
  420.  
  421. // Function: ViewerSetActiveModeless - Set handle of active modeless dlg
  422. // --------------------------------------------------------------------
  423. // Parameters: WPARAM   wParam          WA_ flag defined in Windows.h
  424. //             HWND     hModelessDlg    Handle of dlg being activated or
  425. //                                      deactivated
  426. //
  427. // Returns:    VOID
  428. // --------------------------------------------------------------------
  429. VOID FAR ViewerSetActiveModeless( WPARAM wParam, HWND hModelessDlg )
  430.  
  431. {
  432.     if( wParam != WA_INACTIVE )
  433.         g.hActiveModelessDlg = hModelessDlg;
  434.     else if( g.hActiveModelessDlg == hModelessDlg )
  435.         g.hActiveModelessDlg = NULL;
  436.  
  437.     return;
  438. }
  439.  
  440. // Function: ViewerNoMoreWindow - Sets global handles to NULL. Called during
  441. //                                frame window WM_DESTROY message processing
  442. //                                This is needed for accelerator tests in 
  443. //                                main message loop after window is destroyed
  444. // --------------------------------------------------------------------
  445. // Parameters: VOID
  446. //
  447. // Returns:    VOID
  448. // --------------------------------------------------------------------
  449. VOID FAR ViewerNoMoreWindow( VOID )
  450.  
  451. {
  452.     g.hwndFrame  = NULL;
  453.     g.hwndClient = NULL;
  454.  
  455.     return;
  456. }
  457.  
  458.  
  459. ///  The remaining functions are the query functions called by other modules
  460.  
  461. // Function: ViewerQueryInstance - Query Instance Handle
  462. // --------------------------------------------------------------------
  463. // Parameters: None.
  464. //
  465. // Returns:    HINSTANCE hInstance;    Application instance handle
  466. // --------------------------------------------------------------------
  467. HINSTANCE FAR ViewerQueryInstance( VOID )
  468.  
  469. {
  470.     return g.hInstance;
  471. }
  472.  
  473. // Function: ViewerQueryResources - Query Resource-Only DLL Handle
  474. // --------------------------------------------------------------------
  475. // Parameters: None.
  476. //
  477. // Returns:    HINSTANCE hResources    Resource-only DLL handle
  478. // --------------------------------------------------------------------
  479. HINSTANCE FAR ViewerQueryResources( VOID )
  480.  
  481. {
  482.     return g.hResources;
  483. }
  484.  
  485. // Function: ViewerQueryFrameWindow - Query Frame Window Handle
  486. // --------------------------------------------------------------------
  487. // Parameters: None.
  488. //
  489. // Returns:    HWND hwndFrame;          Frame window handle
  490. // --------------------------------------------------------------------
  491. HWND FAR ViewerQueryFrameWindow( VOID )
  492.  
  493. {
  494.     return g.hwndFrame;
  495. }
  496.  
  497. // Function: ViewerIsPalettized - Are we running on a palettized device?
  498. // --------------------------------------------------------------------
  499. // Parameters: None.
  500. //
  501. // Returns:    BOOL fPalettized;        TRUE if palettized device
  502. // --------------------------------------------------------------------
  503. BOOL FAR ViewerIsPalettized( VOID )
  504.  
  505. {
  506.     return g.fPalettized;
  507. }
  508.  
  509. // Function: ViewerQueryOleData - Query -> to Ole Data struct
  510. // --------------------------------------------------------------------
  511. // Parameters: None.
  512. //
  513. // Returns:    LPQTOLE_OLEDATA        -> ole data struct
  514. // --------------------------------------------------------------------
  515. LPQTOLE_OLEDATA FAR ViewerQueryOleData( VOID )
  516.  
  517. {
  518.     return &g.qtoleOleData;
  519. }
  520.  
  521.  
  522.